home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-05-21 | 2.5 KB | 115 lines | [TEXT/ttxt] |
- --<<<
-
- -- Callback example
-
-
-
- -- put on the kettle
- -- the time the kettle takes to boil depends on
- -- how many cups of water you are boiling
- -- while the kettle is heating up, it hums
- -- when it starts boiling, it whistles
-
-
- class Kettle (RootObject)
- instance variables
- heatingTimePerCup:3
- end
-
-
- -- clock1 is the clock
- -- cups is the number of cups of water to heat
- -- t is the number of ticks from now to start the kettle
- -- onceOnly is true or false depending on whether you
- -- want the callback to cancel itself after its first invocation
-
- method scheduleKettle self {class Kettle} clock1 cups t label onceOnly ->
- (
- addTimeCallback clock1 putOnKettle self #(cups, clock1) \
- (clock1.time + (t as time)) onceOnly
- )
-
-
-
- method putOnKettle self {class Kettle} cups clock1 ->
- (
- local t := clock1.time
- format debug "Putting on the kettle at time %* \n" t @normal
- local timePerCup := self.heatingTimePerCup
- local timeToBoil := (timePerCup * cups) as time
-
- -- create a callback to schedule when the kettle boils
- -- the callback will cancel itself when it has been invoked
- local tb := addTimeCallBack clock1 startBoiling self \
- #(clock1) (clock1.time + timeToBoil) true
-
- -- start heating the kettle
- heatUpKettle self clock1
- )
-
-
- -- while the kettle is heating up, it hums
- -- use the prin method as the action for the
- -- callback that does the humming
-
- method heatUpKettle self {class Kettle} clock1 ->
- (
- print "Starting to heat up the kettle "
-
- local cb := addPeriodicCallback clock1 \
- prin "hum \n" #(@unadorned, debug) 1
-
- -- give the callback a label so we can find it later
- cb.label := "hum"
- )
-
-
- -- when the kettle boils, it whistles
- method startBoiling self {class Kettle} clock1 ->
- (
- local t := clock1.time
- format debug "Starting to boil at time %*\n" t @normal
-
- -- cancel the callback that makes the hum play
- local humCB := chooseOne clock1.callbacks \
- (a b -> a.label = "hum") 1
- cancel humCB
-
- -- the kettle whistles
- whistle self
-
-
-
-
- )
-
-
-
- method whistle self {class kettle} ->
- (print "Whistle, whistle, whistle !!!!!!!!!!!")
-
-
-
- -- Test the kettle simulation
- global clock1 := new clock
- clock1.rate := 1
-
- -- put the kettle on in five seconds to heat 3 cups of water
- -- since OnceONly is true, the callback will cancel itself
- -- after it is invoked
-
- global kettle1 := new Kettle
- scheduleKettle kettle1 clock1 3 5 "cb1" true
-
- -- schedule the kettle to be put on again in 20 seconds
- -- to heat 6 cups of water
- -- again, the cancel will clean itself up when it is finished
- scheduleKettle kettle1 clock1 6 20 "cb1" true
-
-
-
-
-
-
- -->>>
-